Thread
- TestThread类 继承Thread类
- 重写run()方法
- new TestThread().start() 启动一个新的线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class TestThread extends Thread{ public void run(){ while(true){ System.out.println(Thread.currentThread()); } } } public class ThreadDemo1 { public static void main(String[] args){ new TestThread().start(); new TestThread().run(); while(true){ System.out.println(Thread.currentThread()); } } }
|
Thread 匿名内部类实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class ThreadDemo2 { public static void main(String[] args) { Thread t = new Thread(){ public void run(){ while(true) System.out.println(Thread.currentThread()); } }; t.start(); while(true) System.out.println(Thread.currentThread()); } }
|
Runnable
- TestThread类 实现Runnable接口
- 实现run方法
- TestThread t = new TestThread()
- new Thread(t).start() 启动一个新的线程 (比Thread方法多一步)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class TestRunnable implements Runnable{ public void run(){ while(true) System.out.println(Thread.currentThread()); } } public class ThreadDemo3 { public static void main(String[] args) { TestRunnable tt = new TestRunnable(); Thread t = new Thread(tt); t.start(); while(true) System.out.println(Thread.currentThread()); } }
|
Runnable 匿名内部类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class ThreadDemo4 { public static void main(String[] args) { Runnable r = new Runnable(){ public void run(){ while(true) System.out.println(Thread.currentThread()); } }; new Thread(r).start(); while(true) System.out.println(Thread.currentThread()); } }
|
Thread 和 Runnable 区别
Thread
一个线程对象无论多少次start(),只能启动一次。
Runnable
适合多个相同程序代码的线程,去处理同一份资源
可以避免单继承的局限
例子
一个资源,两个线程做++操作,两个线程做–操作,且同步。
Thread 实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class ThreadDemo200 { int size = 100; public synchronized void increase(){ System.out.println(size++ + Thread.currentThread().getName()); } public synchronized void decrease(){ System.out.println(size-- + Thread.currentThread().getName()); } class T1 extends Thread{ public void run(){ while(true) increase(); } } class T2 extends Thread{ public void run(){ while(true) decrease(); } } public static void main(String[] args){ ThreadDemo200 t = new ThreadDemo200(); t.new T1().start(); t.new T1().start(); t.new T2().start(); t.new T2().start(); } }
|
Runnable 实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| public class ThreadDemo100 { int size = 100; public synchronized void increase(){ System.out.println(size++ + Thread.currentThread().getName()); } public synchronized void decrease(){ System.out.println(size-- + Thread.currentThread().getName()); } class R1 implements Runnable{ public void run(){ while(true) increase(); } } class R2 implements Runnable{ public void run(){ while(true) decrease(); } } public static void main(String[] args) { ThreadDemo100 t = new ThreadDemo100(); Runnable r1 = t.new R1(); Runnable r2 = t.new R2(); new Thread(r1).start(); new Thread(r1).start(); new Thread(r2).start(); new Thread(r2).start(); } }
|